/*
 * Copyright 2010 Nicolas Lochet Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */
package org.nicocube.wol.game.client.sub;

import java.util.Date;

import org.nicocube.gwt.util.client.form.Form;
import org.nicocube.gwt.util.client.form.FormVisitor;
import org.nicocube.gwt.util.client.form.NamedWidgetProvider;
import org.nicocube.gwt.util.client.gender.Gender;
import org.nicocube.gwt.util.client.pubsub.Subscriber;
import org.nicocube.wol.game.client.LogSubServiceAsync;
import org.nicocube.wol.game.client.i18n.I18nSubscriberPanel;
import org.nicocube.wol.resource.client.WolBundle;
import org.nicocube.wol.ui.client.MyFlexTable;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public abstract class SubscribePanel extends Composite {

	private I18nSubscriberPanel constants;
	private WolBundle bundle = GWT.create(WolBundle.class);

	private final LogSubServiceAsync logSubServiceAsync;

	private final VerticalPanel vp;
	private final DecoratorPanel dp;

	private MyFlexTable formData;
	public SubscribePanel(LogSubServiceAsync playerRegistrationServiceAsync) {
		this.logSubServiceAsync = playerRegistrationServiceAsync;
		constants = GWT.create(I18nSubscriberPanel.class);
		vp = new VerticalPanel();
		dp = new DecoratorPanel();
		dp.addStyleName(bundle.css().wolSubscribePanel());
		dp.add(vp);
		formData = new MyFlexTable();
		initWidget(dp);
	}

	@Override
	protected void onLoad() {
		super.onLoad();

		final PlayerForm playerForm = new PlayerForm();
		playerForm.setGender(Gender.Unknown);
		playerForm.setDateOfBirth(new Date());

		playerForm.visitFormFields(constants,new FormVisitor() {
			@Override
			public void visit(NamedWidgetProvider f) {
				formData.addRow(new Label(constants.fieldsLabel().get(f.getName())),f.getWidget());
			}
		});

		vp.add(formData);

		Button button = new Button(constants.sendSubscription());
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				//
				if (playerForm.checkPassword()) {
					Window.alert(constants.passwordDifferent());
					playerForm.setPassword("");
					playerForm.setRetypePw("");
				} else
				//
				if (Window.confirm(constants.sureToSave())) {
					logSubServiceAsync.submit(playerForm, new AsyncCallback<Void>() {
						@Override
						public void onFailure(Throwable caught) {
							Window.alert("error:"+caught.getMessage());
						}
						@Override
						public void onSuccess(Void result) {
							Window.alert(constants.successSave());
							onSubscribe();
						}
					});
				}
			}
		});

		vp.add(button);

		final Label l = new Label();

		playerForm.subscribe(new Subscriber<Form>() {
			@Override
			public void update(Form publisher) {
				l.setText(publisher.toString());
			}
		});

		vp.add(l);
	}

	public abstract void onSubscribe();
	
}
